<!DOCTYPE html>
<?php require dirname(dirname(__FILE__)) . '/view/checkuser.php';?>
<html>
<head>
<meta charset="UTF-8">
<title>188对比</title>
<link rel="stylesheet" type="text/css" href="../include/css/base.css" />
<link href="/include/css/smoothness/jquery-ui-1.10.3.custom.min.css" rel="stylesheet">
<script type="text/javascript" src="/include/js/jquery-1.9.1.js"></script>
<script type="text/javascript" src="/include/js/jquery-ui-1.10.3.custom.min.js"></script>
<script type="text/javascript" src="/include/js/jquery.ui.datepicker-zh-cn.js"></script>
<style>
/*提示圆角框*/
.circle_bot { clear:both; font: 12px/1.125 Arial; }
.circle_bot .s_b b { font-size:1px; line-height:1px; overflow:hidden; display:block; clear:both; }
.circle_bot .s_b b, .circle_bot .info { background:#fff2f2; border:#f88 solid; }
.circle_bot b.b1 { border-width:1px 0 0 0; margin:0 2px; height:0px; }
.circle_bot b.b2 { border-width:0 1px; margin:0 1px; height:1px; }
.circle_bot .info { border-width:0 1px; color:#333; padding:10px; text-align:center;}
</style>
<script type="text/javascript">
var chech_new_loop, check_new;

$(function(){
    $("#time_start").datepicker({
        buttonImage: "images/calendar.gif",
        buttonImageOnly: true,
        changeMonth: true,
        changeYear: true
    });
    $("#time_end").datepicker({
        buttonImage: "images/calendar.gif",
        buttonImageOnly: true,
        changeMonth: true,
        changeYear: true
    });

    check_new = function(){
    	$.getJSON("/view/hasnew_half_comp_188.php?"+Date.parse(new Date()), function(re){
    		if (re.re == 'succ'){
    			$("#reminder").show();
    			$("#msgsound").html('<embed src="/include/global.wav" autostart="true" hidden="true">');
    			window.clearInterval(chech_new_loop);
    		}
    	});
    }
    check_new();
    chech_new_loop = window.setInterval('check_new()', 5000);
    
});
</script>
</head>
<body>
<?php
$nowtime = time();
$readnew = isset($_GET['readnew']) ? $_GET['readnew'] : 'n';
$time_start = isset($_GET['time_start']) ? $_GET['time_start'] : date('Y-m-d', $nowtime);
$time_end = isset($_GET['time_end']) ? $_GET['time_end'] : date('Y-m-d', $nowtime);

$time_start_stamp = strtotime($time_start);
$time_end_stamp = strtotime($time_end);
if ($time_start_stamp == false || $time_end_stamp == false){
	exit('时间参数错误！');
}
if ((($time_end_stamp - $time_start_stamp) / 86400) >= 10){
	exit('最多支持10天数据查询！');
}

require dirname(dirname(__FILE__)) . '/include/conf.php';
require dirname(dirname(__FILE__)) . '/include/basefunc.php';
require dirname(dirname(__FILE__)) . '/include/database.php';

$db = db_touzhu();

if ($_SERVER['REQUEST_METHOD'] == 'POST'){
	foreach ($_POST as $_key => $one){
		if (!is_numeric($one)){
			continue;
		}
		@list($_postitem, $_id) = explode('_', $_key);
		if ($_postitem == 'matchstatus' && is_numeric($_id) && $_id > 0){
			$sql_up = "update match_status set result_score={$one} where id={$_id}";
			$db->query($sql_up);
		}
	}
	exit('成功！<a href="/view/half_comp_188.php">返回</a>');
}
?>
<div id="reminder" style="display: none">
	<div id="msgsound"></div>
	<div class="circle_bot">
		<span class="s_b"><b class="b1"></b><b class="b2"></b></span>
		<div class="info"><a href="half_comp_188.php?readnew=y&time_start=<?php echo $time_start?>&time_end=<?php echo $time_end?>">有新条目</a></div>
		<span class="s_b"><b class="b2"></b> <b class="b1"></b></span>
	</div>
</div>

<form action="" method="get">
日期：
<input type="text" id="time_start" name="time_start" value="<?php echo $time_start?>" size="8" /> - 
<input type="text" id="time_end" name="time_end" value="<?php echo $time_end?>" size="8" />
<input type="hidden" name="readnew" value="<?php echo $readnew?>">
<input type="submit" value=" GO " />
<a href="?readnew=y&time_start=<?php echo $time_start?>&time_end=<?php echo $time_end?>">符合模型情况</a>
</form>

<form action="" method="post">
<table class="tableunify">
	<thead>
		<tr>
			<th>联赛名称</th>
			<th>主队</th>
			<th>客队</th>
			<th>比赛开始时间</th>
			<th>盘口</th>
			<th>大水</th>
			<th>小水</th>
			<th>抓取时间1</th>
			<th>盘口</th>
			<th>大水</th>
			<th>小水</th>
			<th>抓取时间2</th>
			<th>盘口</th>
			<th>大水</th>
			<th>小水</th>
			<?php if($readnew != 'y'){?>
			<th>早减晚</th>
			<th>比赛结果</th>
			<?php }else {?>
			<th>模型</th>
			<th>结论</th>
			<th>比赛结果</th>
			<?php }?>
		</tr>
	</thead>
	<tbody>
	<?php
	$time_start_sql = date('Y-m-d 12:00:00', $time_start_stamp);
	$time_end_sql = date('Y-m-d 11:59:59', $time_end_stamp + 86400);
	$sql = "select a.id, a.from, a.competition_id, a.competition, a.match_id, a.home_team_id, a.home_team, a.visiting_team_id, a.visiting_team,";
	$sql .= "a.result_score, a.add_time, a.status, b.id as hid, b.type, b.playing_time, b.pankou, b.over, b.under, b.fetch_time, b.model_num, b.conclusion, b.bet_money, b.unread ";
	$sql .= "from match_status as a left join half_compare as b on b.match_id=a.match_id and a.`from`=b.`from` where a.`from`=2 and ";
	$sql .= "a.add_time>='{$time_start_sql}' and a.add_time<='{$time_end_sql}' order by a.id asc";
	$re = $db->query($sql);
	
	if (!empty($re)){
		$compare_arr = $read_ids = array();
		foreach ($re as $per){
			$key_match = $per['match_id'];
			if (!isset($compare_arr[$key_match])){
				$compare_arr[$key_match]['id'] = $per['id'];
				$compare_arr[$key_match]['competition'] = $per['competition'];
				$compare_arr[$key_match]['home_team'] = $per['home_team'];
				$compare_arr[$key_match]['visiting_team'] = $per['visiting_team'];
				$compare_arr[$key_match]['result_score'] = $per['result_score'];
			}
			$compare_arr[$key_match]['fetch_time_'.$per['type']] = $per['fetch_time'];
			$compare_arr[$key_match]['playing_time_'.$per['type']] = $per['playing_time'];
			$compare_arr[$key_match]['pankou_'.$per['type']] = $per['pankou'];
			$compare_arr[$key_match]['over_'.$per['type']] = $per['over'];
			$compare_arr[$key_match]['under_'.$per['type']] = $per['under'];
			$compare_arr[$key_match]['model_num_'.$per['type']] = $per['model_num'];
			$compare_arr[$key_match]['unread_'.$per['type']] = $per['unread'];
			$compare_arr[$key_match]['bet_money_'.$per['type']] = $per['bet_money'];
			$compare_arr[$key_match]['conclusion_'.$per['type']] = $per['conclusion'];
			
			if ($readnew == 'y' && $per['unread'] == 1){
				$read_ids[] = $per['hid'];
			}
		}
		
		// 设置为已读
		if (!empty($read_ids)){
			$read_ids_str = implode(',', $read_ids);
			$up_read_sql = "update half_compare set unread=2 where id in({$read_ids_str})";
			$db->query($up_read_sql);
		}
		
		$counter = 0;
		foreach ($compare_arr as $val){
			// 最早大水减最晚大水
			if (isset($val['over_3']) && isset($val['over_4']) && is_numeric($val['over_3']) && is_numeric($val['over_4'])){
				$compare_over = round(($val['over_3'] - $val['over_4']), 2);
			}else {
				$compare_over = '-';
			}

			$model_num = (isset($val['model_num_4']) && $val['model_num_4'] > 0) ? $val['model_num_4'] : 0;
			if ($readnew == 'y' && $model_num == 0){
				continue;
			}
			$str_model_num = (isset($val['unread_4']) && $val['unread_4'] == 1) ? ($model_num . ' <span style="color:red;">new</span>') : ($model_num > 0 ? $model_num : '');
			
			$is_alt = ($counter ++ % 2) ? ' class="alt"' : '';
			echo "<tr {$is_alt}><td>{$val['competition']}</td><td>{$val['home_team']}</td><td>{$val['visiting_team']}</td>",
			
				'<td title="比赛时间：', isset($val['playing_time_1']) ? $val['playing_time_1'] : '', '">',
				isset($val['fetch_time_1']) ? substr($val['fetch_time_1'], 5) : '-',
				'</td><td>', isset($val['pankou_1']) ? $val['pankou_1'] : '-',
				'</td><td>', isset($val['over_1']) ? $val['over_1'] : '-',
				'</td><td>', isset($val['under_1']) ? $val['under_1'] : '-', '</td>';
				
			echo '<td title="比赛时间：', isset($val['playing_time_3']) ? $val['playing_time_3'] : '', '">',
				isset($val['fetch_time_3']) ? substr($val['fetch_time_3'], 5) : '-',
				'</td><td>', isset($val['pankou_3']) ? $val['pankou_3'] : '-',
				'</td><td>', isset($val['over_3']) ? $val['over_3'] : '-',
				'</td><td>', isset($val['under_3']) ? $val['under_3'] : '-', '</td>';
				
			echo '<td title="比赛时间：', isset($val['playing_time_4']) ? $val['playing_time_4'] : (isset($val['playing_time_7']) ? $val['playing_time_7'] : ''), '">',
				isset($val['fetch_time_4']) ? substr($val['fetch_time_4'], 5) : (isset($val['fetch_time_7']) ? '<span style="color:green;">'.substr($val['fetch_time_7'], 5).'</span>' : '-'),
				'</td><td>', isset($val['pankou_4']) ? $val['pankou_4'] : (isset($val['pankou_7']) ? $val['pankou_7'] : '-'),
				'</td><td>', isset($val['over_4']) ? $val['over_4'] : (isset($val['over_7']) ? $val['over_7'] : '-'),
				'</td><td>', isset($val['under_4']) ? $val['under_4'] : (isset($val['under_7']) ? $val['under_7'] : '-'), '</td>';
			
			$result_score = $val['result_score'] > -1 ? $val['result_score'] : '';
			if($readnew != 'y'){
				echo "<td>{$compare_over}</td><td style=\"padding:1px;\">" . $result_score,
			    	'<input type="text" name="matchstatus_' . $val['id'] . '" value="" size="1" /></td>';
			}else {
				echo '<td><a href="/view/bet_model.php">' . $str_model_num . '</a></td><td style="color:green;">' . $val['conclusion_4'] . ' | ' . $val['bet_money_4'] . '</td>',
					"<td>{$result_score}</td>";
			}
			
			echo "</tr>\n";
		}
		
	}
	
	$page_query = "time_start={$time_start}&time_end={$time_end}";
	?>
	</tbody>
	<?php if($readnew != 'y'){?>
	<tfoot>
		<tr>
		   <th class="paging" colspan="17">
		   	 <a href="?<?php echo $page_query?>">刷新</a> | 
		     <input type="submit" value=" 保 存 " />
		   </th>
		</tr>
	</tfoot>
	<?php }?>
</table>
</form>

</body>
</html>